package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.User_managementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
* 新增用户管理
* */
@RestController
@RequestMapping("/user_management")
public class User_managementController {
    @Reference//查找服务
    private User_managementService user_managementService;
    @Autowired//加密
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user,Integer[] roles){
        try{
            String password = user.getPassword();
            password = bCryptPasswordEncoder.encode(password);
            user.setPassword(password);
            user_managementService.add(user,roles);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
       return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }
    //用户分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = user_managementService.pageQuery(queryPageBean);
        return pageResult;
    }
    //删除用户
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            user_managementService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }
    //编辑用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user,Integer[] roles){
        try{
            user_managementService.edit(user,roles);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }
    //查询用户
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            User user = user_managementService.findById(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,user);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }
    //根据userId 查询roleIds
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id) {
        List<Integer> roleIds = user_managementService.findRoleIdsByUserId(id);
        return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleIds);
    }
}
